//
//  Card.m
//  SCRAP_ObjC
//
//  Created by Capstone Design Fall 2010 on 10/20/10.
//  Copyright 2010 Thunderwear. All rights reserved.
//

#import "Card.h"
#import "Constants.h"


@implementation Card

@synthesize touch, touchOffset, stackRef, rank, suit, isReserve, holdPos, _shadow;

+ (Card *) getRandomCard {
	int rank = arc4random() % 13 + 1;
	int s = arc4random() % 4;
	char suit;
	switch (s) {
		case 0:
			suit='h';
			break;
		case 1:
			suit='s';
			break;
		case 2:
			suit='d';
			break;
		case 3:
			suit='c';
			break;
		default:
			break;
	}
	
	Card * card = [[Card alloc] initWithRank:rank withSuit:suit];
	card.scale = .5;
	return card;
}

+(Card*) initWithRank:(int)r withSuit:(char)s forIPad:(id)x {
	Card * c = [[Card alloc] initWithRank:r withSuit:s];
//	c.scale = 1.5;
	return c;
}

+(Card*) initWithRank:(int)r withSuit:(char)s forIPhone:(id)x {
	Card * c = [[Card alloc] initWithRank:r withSuit:s];
	c.scale = iPhoneScale;
	return c;
}
	
-(Card*) initWithRank:(int)r withSuit:(char)s
{
//	if (cardSheet==nil) {
//		cardSheet = [CCSpriteSheet spriteSheetWithFile:@"card_sheet.png"];
//	}
	
	NSMutableString* fileName = [NSMutableString stringWithString:@""];
	switch (r) {
		case 1: [fileName appendString: @"A_"];
			break;
		case 11: [fileName appendString: @"J_"];
			break;
		case 12: [fileName appendString: @"Q_"];
			break;
		case 13: [fileName appendString: @"K_"];
			break;
		default:
			if (r>1 && r<11) {
				[fileName appendString:[NSString stringWithFormat:@"%d_",r]];
			}
	}

	NSString *str = [NSString stringWithFormat:@"%c", s];
	
	[fileName appendFormat:@"%@.png", str];
	
	float width = 91;
	float height = 135;
	float space=5; // from sprite sheet
	float startX, startY;
	int i, j=0;
	
	// find index for rank
	if (r>=2 && r<=13) {
		i = r-2;
	} else {
		i = 12;
	}
	// find index for suit, be clever with a switch statement
	switch (s) {
		case 'd': j++;
		case 'h': j++;
		case 'c': j++; break;
		default: j=0; break;
	}
	
	startX = i*(width+space);
	startY = j*(height+space);
	
	// always call "super" init
	// Apple recommends to re-assign "self" with the "super" return value
	if( (self= [super initWithFile:fileName])){//[super initWithTexture:cardSheet.texture rect:CGRectMake(startX, startY, width, height)] )) {
		//[cardSheet retain];
		rank = r;
		suit = s;
		/*_shadow = [CCSprite spriteWithFile:@"shadow.png"];
		_shadow.opacity = 0;
		// NSLog(@"Card is %f wide, %f high\n", [self boundingBox].size.width, [self boundingBox].size.height);
		[_shadow setPosition: ccp([self boundingBox].size.width/2, [self boundingBox].size.height/2)];
		[self addChild:_shadow z: -1];*/
		_outline = [CCSprite spriteWithFile:@"card_outline.png"];
		[_outline setPosition: ccp([self boundingBox].size.width/2, [self boundingBox].size.height/2)];
		[self addChild:_outline z:-1];
	}
	return self;
}

-(BOOL) isRed {
	return suit == 'h' || suit=='d';
}

-(BOOL) isBlack {
	return ![self isRed];
}

-(void) removeShadow {
	/*id action = [CCFadeTo actionWithDuration:.1 opacity:0];
	id ease = [CCEaseInOut actionWithAction:action rate:2];
	[_shadow runAction:ease];*/
}

-(void) setShadow:(int)_opacity {
	/*id action = [CCFadeTo actionWithDuration:.1 opacity:_opacity];
	id ease = [CCEaseInOut actionWithAction:action rate:2];
	[_shadow runAction:ease];*/
}

-(NSString *) description {
	return [NSString stringWithFormat:@"Rank %d, Suit %c", rank, suit];
}

-(BOOL) pointInside:(CGPoint)point {

	return CGRectContainsPoint([self boundingBox], point);
}

// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
	// in case you have something to dealloc, do it in this method
	// in this particular example nothing needs to be released.
	// cocos2d will automatically release all the children (Label)
	
	
	//[cardSheet release]; ??????
	// don't forget to call "super dealloc"
	[super dealloc];
}
	
@end
